#include "createtable.h"


CreateTable::CreateTable()
{
    model = null;
}

CreateTable::~CreateTable()
{
    delete model;
}

///Function to make a table
QStandardItemModel * CreateTable::MakeTable(int iInputWeight, int iInputSteps, int iInputEndWeight, float fInputLenght, QObject *object)
{
    QuickCalculation *QCcalc = new QuickCalculation();

    int iCount = 1;
    int iTempWeight = iInputWeight;
    //Loop while iTempWeight  is smaller ten iInputEndWeight. This will calculate te total ammount of steps taken to reac te endweight
    while(iTempWeight < iInputEndWeight)
    {
        iCount++;
        iTempWeight += iInputSteps;
    }

    //clear instance of model
    if(model != null)
    {
        model->clear();
        delete model;
    }

    //Make a table using a QstandardItemModel, iCount being the total amount of rows thas was calculated, 4 coloms
    model = new QStandardItemModel(iCount,4,object);
    //Very first row (0) on colum position 0 the header is named Weight. Weight values will be put in this colum.
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("Weight"))); //Top row: colom 0 weight
    //Very first row (0) on colum position 1 the header is named Lenght. Lenght values will be put in this colum.
    model->setHorizontalHeaderItem(1, new QStandardItem(QString("Length"))); //Top row: colom 1 Length
    //Very first row (0) on colum position 2 the header is named Quetelet-index. Quetelet-index values will be put in this colum.
    model->setHorizontalHeaderItem(2, new QStandardItem(QString("Quetelet Index"))); //Top row: colom 2 Quetelet Index
    //Very first row (0) on colum position 3 the header is named verdict. Verdict values will be put in this colum.
    model->setHorizontalHeaderItem(3, new QStandardItem(QString("Verdict"))); //Top row: colom 3 Verdict

    QStandardItem *QSIitem;

    //This loops if i is smaller then iCount
    for(int i = 0; i < iCount; i++)
    {
        //Input weight value + (input steps times i) will be put in iTempWeight
        int iTempWeight = iInputWeight + (iInputSteps * i);
        //Input lenght value is put in fTempLenght
        float fTempLength = fInputLenght;
        //iTempWeight value is put in Data
        struct UserData *Data = new struct UserData();
        //fTempLenght value is put in Data
        Data->iweight = iTempWeight;
        //QueteletIndex calculation calculates the content in Data and puts the result in fQIResult
        Data->flenght = fTempLength;
        //Te Quetelet-index result get's calculated and put into fQIResult
        float fQIResult = QCcalc->QueteletIndexCalculation(Data);
        //Verdict get's determined in ConvertQueteletIndexToString function with the fQIResult value
        QString QSTempVerdict = QCcalc->ConvertQueteletIndexToString(fQIResult);

        QSIitem = new QStandardItem(QString::number(iTempWeight));
        //Set te values in iTempWeight into colom 0. i is te rows it needs to fill per value
        model->setItem(i,0,QSIitem);
        QSIitem = new QStandardItem(QString::number(fTempLength));
        //Set te values in fTempLenght into colom 1. i is te rows it needs to fill per value
        model->setItem(i,1,QSIitem);
        QSIitem = new QStandardItem(QString::number(fQIResult));
        //Set te values in fQIResult into colom 2. i is te rows it needs to fill per value
        model->setItem(i,2,QSIitem);
        QSIitem = new QStandardItem(QSTempVerdict);
       //Set te values in QSTempVerdict into colom 3. i is te rows it needs to fill per value
        model->setItem(i,3,QSIitem);
    }

    //Delete QCcalc instance
    delete QCcalc;
    return model;
}

//Function to open a saved table
QStandardItemModel * CreateTable::OpenTable(QString sFileName, QObject *object)
{
    QFile file(sFileName);
    if(!file.open(QIODevice::ReadOnly))
    {
        //Error if file not found/can not open
        QMessageBox::information(0,"error",file.errorString());
    }
    //Read line
    file.readLine();

    char cReadChar[10];
    file.readLine(cReadChar, 10);
    //Convert array to integer
    int iReadWeight = atoi(cReadChar);

    file.readLine(cReadChar, 10);
    //Convert array to charracter
    float fReadLength = atof(cReadChar);

    file.readLine(cReadChar, 10);
    //Convert array to integer
    int iReadSteps = atoi(cReadChar);

    file.readLine(cReadChar, 10);
    //Convert array to integer
    int iReadEndWeight = atoi(cReadChar);

    return MakeTable(iReadWeight, iReadSteps, iReadEndWeight, fReadLength, object);
}
